package com.xy.type3;

/**
 * @author 谢阳
 * @version 1.8.0_131
 * @date 2022/6/9 18:46
 * @description 单例模式 --》 懒汉式（线程不安全）
 */
public class SingletonTest03 {
    public static void main(String[] args) {
        //for (int i = 0; i < 10; i++) {
        //    new Thread(new Runnable() {
        //        @Override
        //        public void run() {
        //            System.out.println(Singleton.getInstance().hashCode());
        //        }
        //    }).run();
        //}
        
        Singleton instance1 = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance1 == instance2);
        System.out.println("instance1.hashCode() = " + instance1.hashCode());
        System.out.println("instance2.hashCode() = " + instance2.hashCode());


    }
}

class Singleton {
    private static Singleton instance;

    private Singleton(){}

    /**
     * 提供静态方法，当使用方法时，才去创建instance
     * @return
     */
    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }

        return instance;
    }

}